package cn.zhejianglao.shirotemplate.service.menu;

import cn.zhejianglao.shirotemplate.common.model.Response;
import cn.zhejianglao.shirotemplate.common.model.MenuVO;
import cn.zhejianglao.shirotemplate.common.validator.ParamValidator;
import cn.zhejianglao.shirotemplate.service.menu.service.IMenuService;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/menu")
public class MenuController {
    @Autowired
    private IMenuService menuService;

    @RequiresRoles("admin")
    @GetMapping("/list/{parentId}")
    public Response<List<MenuVO>> list(@PathVariable("parentId") int parentId) {
        return Response.success(menuService.list(parentId));
    }

    @RequiresRoles("admin")
    @PostMapping("/create")
    public Response<String> create(@RequestBody MenuVO vo) {
        ParamValidator.validate(vo);
        menuService.create(vo);
        return Response.success();
    }

    @RequiresRoles("admin")
    @DeleteMapping("delete/{id}")
    public Response<String> delete(@PathVariable("id") Long id) {
        menuService.delete(id);
        return Response.success();
    }

    @RequiresRoles("admin")
    @PostMapping("/update")
    public Response<String> update(@RequestBody MenuVO vo) {
        ParamValidator.validate(vo);
        menuService.update(vo);
        return Response.success();
    }
}
